package controller;

import java.util.List;

import com.jfinal.core.Controller;

import model.Admin;
import model.User;
/**
 * 出来登录请求验证
 */
public class LoginController extends Controller {
	
	public String name;//获取输入用户名
	public String password;//获取输入密码
	public String password1;//确认密码
	public void index(){
		
		render("welcome/login.jsp");
	}
	
	/**
	 * 用户登录验证
	 */
	public void submit() {
		name = getPara("username");
		password = getPara("password");
		List<Admin> admin = Admin.dao.find("select * from admin where name='" + name + "' and password='" + password + "'");
		setAttr("admin", admin);
		if(admin.size()>0)
		{
			System.out.println("登陆成功");
			List<User> users = User.dao.find("select * from user");
			setAttr("users",users);
			render("user/list.jsp");
		}else{
			System.out.println("登陆失败");
			index();
		}
	}

	/**
	 * 用户注册
	 */
	public void register() {

		render("welcome/register.jsp");
	}
	/**
	 * 用户添加
	 */
	public void add() { 
		
		password1 = getPara("admin.password");
		password = getPara("admin.password");
		List<Admin> admin1 = Admin.dao.find("select * from admin where name='" + name + "'");
		
		if(password.equals(password1))
		{
			register();
		}
		else if(admin1.size()<0){
		Admin admin = getModel(Admin.class, "admin");
		admin.save();
		System.out.println("注册成功");
		index();
		}
		else{
			register();
		}
	}
	
	public void test(){
		
		render("test.jsp");
		
	}
	
}
